home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / movefile.zip / EXPARG.C < prev    next >
Text File  |  1993-01-04  |  4KB  |  141 lines

  1.  
  2. /*  exparg.c  source code for exparg()
  3.  */
  4.  
  5. /******************************************************************************
  6.  
  7.   NAME    exparg - expand filespec command line parameters
  8.          containing wild-card characters
  9.  
  10.   USAGE   #include "exparg.h"
  11.       char **exparg (int *pargc, char *argv []);
  12.  
  13.   PROTOTYPE IN  exparg.h
  14.  
  15.   DESCRIPTION Exparg is used to explicitly process the filespec
  16.       command line parameters containing wild-card
  17.       characters. It takes a pointer to an integer
  18.       containing the count of the parameters and a pointer
  19.       to an array of pointers to the strings copied from
  20.       the comamnd line. It is anticipated that most of the
  21.       time exparg() will be called immediately upon entry
  22.       to main(), with the call looking like
  23.  
  24.         argv = exparg (&argc, argv);
  25.  
  26.       See the test program included in this file for an
  27.       example usage.
  28.  
  29.       When exparg() finds a filespec parameter string
  30.       containing wild-card characters, it replaces the
  31.       string with the name of the first file that matches
  32.       it and adds the names of subsequent matches to the
  33.       list of parameter string pointers. If no matching file
  34.       names are found, the original string is left in place.
  35.  
  36.       Upon return from exparg() argc will be updated to
  37.       indicate the new number of command line parameters
  38.       and argv will point to a different array of string
  39.       pointers. Command line parameter processing can then
  40.       proceed as it would have been done before.
  41.  
  42.   RETURN VALUE  A pointer to an array of string pointers that point
  43.       to the expanded filespec command line parameters. The
  44.       int containing the parameter count is modified as a
  45.       side-affect.
  46.  
  47.   PORTABILITY MS-DOS specific, Turbo-C specific
  48.  
  49.   AUTHOR    Richard Hargrove
  50.       Texas Instruments, Inc.
  51.       P.O. Box 869305, m/s 8473
  52.       Plano, Texas 75086
  53.       214/575-4128
  54.  
  55.  ******************************************************************************/
  56.  
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <dir.h>
  60. #include <dos.h>
  61. #include <alloc.h>
  62.  
  63. #define MAXARGS   100 /* maximum number of entries the new argv */
  64.         /* array can contain        */
  65.  
  66. #define TRUE    1
  67. #define FALSE   0
  68. #define NIL(type) ((type *) NULL)
  69.  
  70. typedef int BOOLEAN;
  71.  
  72. /******************************************************************************/
  73.  
  74. char **exparg (int *pargc, char *argv [])
  75. {
  76.   static char *newargv [MAXARGS];
  77.   char path [MAXPATH];    /* if the stack doesn't overflow, */
  78.   char drive [MAXDRIVE];    /* we have less lasting impact    */
  79.   char dir [MAXDIR];    /* on the static data segment     */
  80.  
  81.   char far *olddta = getdta ();
  82.   struct ffblk fblk;
  83.   register int args = 0;
  84.   register int newargc = 0;
  85.   BOOLEAN err = FALSE;
  86.  
  87.   while (!err && args < *pargc) {
  88.     if ((fnsplit(argv[args],drive,dir,NIL(char),NIL(char))& WILDCARDS) &&
  89.        (!findfirst (argv [args], &fblk, 0))) {
  90.       do {
  91.         char *localcptr = (char *)malloc (
  92.     (unsigned)(stpcpy (stpcpy (stpcpy (path, drive),
  93.              dir),
  94.            fblk.ff_name) - path) + 1);
  95.         if (localcptr != NIL(char)) {
  96.     newargv [newargc++] = strcpy (localcptr, path);
  97.         }
  98.         else {
  99.     fputs ("\n_exparg error : no memory for filenames\n", stderr);
  100.     exit (1);
  101.         }
  102.       } while ((newargc < MAXARGS) && !findnext (&fblk));
  103.     }
  104.     else {
  105.       newargv [newargc++] = argv [args];
  106.     }
  107.     err = (newargc == MAXARGS);
  108.     args++;
  109.   }
  110.  
  111.   if (err) fputs ("\n_exparg error : too many filenames\n", stderr);
  112.   setdta (olddta);
  113.   *pargc = newargc;
  114.   return (&newargv [0]);
  115. }
  116.  
  117. #ifdef TEST
  118.  
  119. /******************************************************************************/
  120.  
  121. main (int argc, char *argv [])
  122. {
  123.   /*  test exparg()
  124.    */
  125.  
  126.   int i = 0;
  127.  
  128.   printf ("original command line parameters : argc: %d\n", argc);
  129.   for (; i < argc; i++) {
  130.     printf ("%s\n", argv [i]);
  131.   }
  132.  
  133.   argv = exparg (&argc, argv);
  134.  
  135.   printf ("new command line parameters : argc: %d\n", argc);
  136.   for (i = 0; i < argc; i++) {
  137.     printf ("%s\n", argv [i]);
  138.   }
  139. }
  140. #endif
  141.